import { ref } from 'vue'
import { adapter, request } from '@/request'
import { orderManageByGet } from '@/core/api/order'
import type { Order } from '@/core/interfaces/order'
import type { Pagination, RequestWrapper, WithTotalSize } from '@/core/interfaces/common'
import type { PriceSheet } from '@/core/interfaces/priceSheet'
import type { PayStatusEnum, ServiceStatusEnum } from '@/core/enums/status'
import type { Combo, Price } from '@/core/interfaces/combo'
import type { AuthPack } from '@/core/interfaces/base'
import type { TypeEnum } from '@/core/enums/type'
import type { Industry } from '@/core/interfaces/industry'

export interface OrderQuery extends Pagination {
  orderId: Order['id']
  orderNo: Order['orderNo']
  comboName: Combo['comboName'] | string
  comboPriceName: Price['comboPriceName'] | string
  sellerName: AuthPack['sellerName'] | string
  proxyName: AuthPack['proxyName'] | string
  customerId: AuthPack['customerId']
  payStatus: PayStatusEnum
  priceAskId: PriceSheet['id']
  serviceStatus: ServiceStatusEnum
  industryCode: Industry['code']
  keyword: string
  platformNames?: string
  type: TypeEnum
  customerName: string
  productId: number
  buyTime: string
  organize: string
}

export function getOrderListWithAll() {
  const service = adapter<RequestWrapper<WithTotalSize<Record<'orders', Order[]>>>>(request)
  const loading = ref(false)

  const fetch = (params: any) => {
    const { url, method } = orderManageByGet()
    loading.value = true
    return service({
      config: {
        url,
        method,
        params
      },
      done: () => {
        loading.value = false
      }
    })
  }

  return [null, fetch, loading] as const
}

export default function getOrderList() {
  const service = adapter<RequestWrapper<WithTotalSize<Record<'orders', Order[]>>>>(request)
  const loading = ref(false)
  const response = ref<WithTotalSize<Record<'orders', Order[]>>>({
    totalSize: 0,
    orders: []
  })

  const fetch = (params: Partial<OrderQuery>) => {
    const { url, method } = orderManageByGet()
    loading.value = true

    return service({
      config: {
        url,
        method,
        params
      },
      callback: (res) => {
        response.value = res.data.data
      },
      error: (err) => {
        console.log(err)
      },
      done: () => {
        loading.value = false
      }
    })
  }

  return [response, fetch, loading] as const
}
